package Java8实战._1_Lambda._9_Lambda应用;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

public class _9_1_3_Lambda应用_Function {

    public static void main(String[] args) {
        List<String> list = Arrays.asList(
                "qwe",
                "1234567",
                "jiangboyang",
                "1"
        );

        List<Integer> result;

        result = map(list, (String str) -> str.length());
        print(result, (Integer i) -> System.out.println("长度为：" + i));
    }

    public static <T,R> List<R> map(List<T> list, Function<T,R> function){
        List<R> result = new ArrayList<>();
        for (T t : list){
            result.add(function.apply(t));
        }
        return result;
    }

    public static <T> void print(List<T> list, Consumer<T> consumer){

        for (T t : list){
            consumer.accept(t);
        }
    }
}
